//
//  CalendarTool.m
//  etionMisc
//
//  Created by zhaiqiuqiu on 2017/12/9.
//  Copyright © 2017年 GuangZhouXuanWu. All rights reserved.
//

#import "CalendarTool.h"

@interface CalendarTool ()
{
    
}

@property (nonatomic,strong)NSCalendar *chineseCalendar;
@property (nonatomic,strong)NSArray *chineseYearArray;
@property (nonatomic,strong)NSArray *chineseMonthArray;
@property (nonatomic,strong)NSArray *chineseDayArray;

@end

@implementation CalendarTool

- (instancetype)init
{
    self = [super init];
    if(self)
    {
        _chineseCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSChineseCalendar];
        _chineseMonthArray = [NSArray arrayWithObjects:
                              @"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
                              @"九月", @"十月", @"冬月", @"腊月", nil];
        _chineseDayArray = [NSArray arrayWithObjects:
                            @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
                            @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
                            @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",  nil];
    }
    return self;
}

-(NSString*)getChineseDayWithDate:(NSDate*)date
{
    NSInteger tempDay = [self getLocalDayWithDate:date];
    NSString *chineseDay = [_chineseDayArray objectAtIndex:tempDay - 1];
    
    return chineseDay;
}

-(NSString*)getChineseMonthWithDate:(NSDate*)date
{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    
    NSDateComponents *localeComp = [_chineseCalendar components:unitFlags fromDate:date];
    NSString *chineseMonth = [_chineseMonthArray objectAtIndex:localeComp.month - 1];
    
    return chineseMonth;
}

-(NSInteger)getLocalDayWithDate:(NSDate*)date
{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    
    NSDateComponents *localeComp = [_chineseCalendar components:unitFlags fromDate:date];
    NSInteger tempDay = localeComp.day;
    // 系统日历类在2057-09-28计算有bug结果为0（应该为30）
    if(tempDay == 0)
    {
        tempDay = 30;
    }
    return tempDay;
}

- (NSString*)getChineseHolidayWithDate:(NSDate *)date
{
    NSString *holiday = @"";
    
    NSString *chineseMonth = [self getChineseMonthWithDate:date];
    NSString *chineseDay = [self getChineseDayWithDate:date];
    
    if([@"正月" isEqualToString:chineseMonth] &&
       [@"初一" isEqualToString:chineseDay])
    {
        holiday = @"春节";
    }
    else if([@"正月" isEqualToString:chineseMonth] &&
            [@"十五" isEqualToString:chineseDay])
    {
        holiday = @"元宵节";
    }
    else if([@"二月" isEqualToString:chineseMonth] &&
            [@"初二" isEqualToString:chineseDay])
    {
        holiday = @"龙抬头";
    }
    else if([@"五月" isEqualToString:chineseMonth] &&
            [@"初五" isEqualToString:chineseDay])
    {
        holiday = @"端午节";
    }
    else if([@"七月" isEqualToString:chineseMonth] &&
            [@"初七" isEqualToString:chineseDay])
    {
        holiday = @"七夕";
    }
    else if([@"八月" isEqualToString:chineseMonth] &&
            [@"十五" isEqualToString:chineseDay])
    {
        holiday = @"中秋节";
    }
    else if([@"九月" isEqualToString:chineseMonth] &&
            [@"初九" isEqualToString:chineseDay])
    {
        holiday = @"重阳节";
    }
    else if([@"腊月" isEqualToString:chineseMonth] &&
            [@"初八" isEqualToString:chineseDay])
    {
        holiday = @"腊八";
    }
    else if([@"腊月" isEqualToString:chineseMonth] &&
            [@"二四" isEqualToString:chineseDay])
    {
        holiday = @"小年";
    }
    else if([@"腊月" isEqualToString:chineseMonth] &&
            [@"三十" isEqualToString:chineseDay])
    {
        holiday = @"除夕";
    }
    
    return holiday;
}

- (BOOL)isQingMingholidayWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day
{
    if(month == 4)
    {
        NSInteger pre = year / 100;
        float c = 4.81;
        if(pre == 19)
        {
            c = 5.59;
        }
        NSInteger y = year % 100;
        NSInteger qingMingDay = (y * 0.2422 + c) - y / 4;
        if(day == qingMingDay)
        {
            return YES;
        }
    }
    return NO;
}
@end
